﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class my_link_edit : AdminpageBase
{ 
    private string mypath = "";   
    protected Model.Link model=null;
    protected string ctype = ReadRequest.RequestString("t");
    protected long long_eid = ReadRequest.RequestLong("eid"); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            inidata();      
        }
    }
    private void inidata()
    {
        txtctype.Text = ctype;
        try
        { 
            if (ctype != null && ctype.Length > 3 && "edit".Equals(ctype))
            {
                model = BLL.Link.GetModel(long_eid);               
                if (model != null)
                {
                    txturl.Text = model.WebAddress;
                    txtname.Text = model.Title;
                    myimg.ImageUrl = model.PicUrl;
                    myimg1.ImageUrl = model.PicUrl;
                    myimg2.ImageUrl = model.PicUrl;                   
                }
            }
            else
            {
                ctype = "ad";
            }
        }
        catch {
            ctype = "ad";
        }       
        if (long_eid < 1)
        {
            ctype = "ad";
        }
    }

    //submit
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        Up_Image();
    }
    protected void Up_Image()
    {

        bool fileOk = false;
        string d = DateTime.Now.ToString("yyyyMMddHHmmss");
        string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
        string path = Server.MapPath("/userfiles/image/") + d + fileExtension;
        if (FileUpload1.HasFile)
        {
            string[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", "jpeg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOk = true;
                }
            }
            if (fileOk)
            {
                if (myimg.ImageUrl != "/userfiles/temp/default.jpg")
                {
                    Obj.file_delete(myimg.ImageUrl);
                } 
                mypath = "/userfiles/image/" + d + fileExtension; 
                FileUpload1.SaveAs(path);
                addinfo(mypath);
            }
            else
            {
                lblmsg.CssClass = "errmsg";
                lblmsg.Text = "X 图片格式错误。";
            }
        }
        else
        {
            mypath = myimg.ImageUrl;
            addinfo(mypath);
        }
    }
    protected void addinfo(string mypath)
    {
        if ("ad".Equals(txtctype.Text))
        {
            Model.Link Info = new Model.Link(); 
            Info.PicUrl = mypath;
            Info.WebAddress = txturl.Text;
            Info.UpTime = DateTime.Now;
            Info.Title = txtname.Text;
            BLL.Link.Add(Info);
        }
        else
        {
            Model.Link Info = BLL.Link.GetModel(long_eid);
            Info.PicUrl = mypath;
            Info.WebAddress = txturl.Text;
            Info.UpTime = DateTime.Now;
            Info.Title = txtname.Text;
            BLL.Link.Update(Info);
        }
        Response.Redirect("/manage/my_link.aspx");
    }
}